import java.io.IOException;
import java.util.Scanner;

import com.game.cards.BaseCard;
import com.game.cards.Deck;
import com.game.cards.Hand;
import com.game.cards.MonsterCard;
import com.game.cards.decks.*;
import com.game.cards.monsters.Adam;
import com.game.cards.monsters.BlueEyesWhiteDragon;
import com.game.cards.monsters.DarkMagician;
import com.game.cards.monsters.GracelandTempest;
import com.game.cards.monsters.Oscar;
import com.game.play.*;


public class Main {

	public static void clearConsole() throws IOException
	{
		Runtime.getRuntime().exec("cls");
	}
	public static void printStatus(Player c, Player o, Game game)
	{
		System.out.println("Your lifepoints:" + c.getLifePoints());
		System.out.println(o.getName() + "'s lifepoints:" + o.getLifePoints());
		System.out.println("Turn: " + game.getTotalTurns());
		System.out.println();
		
		
	}
	public static void printField(Player c, Player o, Game game)
	{
		System.out.print("Your monsters: ");
		int count = 0;
		for (MonsterCard cd : c.getField().getMonsterCards())
		{
			System.out.println("(" + count +")" + cd.getTitle() + " ATK:" + cd.getAttack() + " DEF:" + cd.getDefense());
			count++;
		}
		System.out.print("\nYour set cards:");
		for (BaseCard bc : c.getField().getSpellTrapCards())
		{
			System.out.println("()" + bc.getTitle());
		}
		System.out.print(o.getName() + "'s monsters: ");
		count = 0;
		for (MonsterCard cd : o.getField().getMonsterCards())
		{
			System.out.println("(" + count +")" + cd.getTitle() + " ATK:" + cd.getAttack() + " DEF:" + cd.getDefense());
			count++;
		}
		System.out.print(o.getName() + "'s set cards: ");
		count = 0;
		for (BaseCard bc : c.getField().getSpellTrapCards())
		{
			System.out.println("(" + count + ")");
		}
		System.out.println();
	
	}
	public static void main(String[] args) throws IOException {
		
		
		ActionQueue aq =  new ActionQueue();
		System.out.println("Welcome to You-Gay-Ho!");
		System.out.println("Please enter the name of Player1");
		Scanner scan = new Scanner(System.in);
		String name = scan.next();
		System.out.println("Please enter the gender of Player1. MALE or FEMALE.");
		String gender = scan.next();
		Player player1 = new Player(name,com.game.play.Gender.valueOf(gender.toUpperCase()),aq);
		System.out.println("Please enter the name of Player2");
		name = scan.next().toUpperCase();
		System.out.println("Please enter the gender of Player2. MALE or FEMALE.");
		gender = scan.next();
		Player player2 = new Player(name,com.game.play.Gender.valueOf(gender.toUpperCase()),aq);
		Game game = new Game(player1,player2);
		aq.setGame(game);
		CommandParser cp = new CommandParser(game,aq,scan);
		int i = 0;
		for (Deck d : game.getDecks())
		{
			System.out.println("(" + i + ")" +d.getTitle());
			i++;
		}
		System.out.println("Player 1 choose a deck (enter the option number)");
		int choice = scan.nextInt();
		player1.setCurrentDeck(game.getDecks().get(choice).clone());
		player1.getCurrentDeck().setOwner(player1);
		i = 0;
		for (Deck d : game.getDecks())
		{
			System.out.println("(" + i + ")" +d.getTitle());
			i++;
		}
		System.out.println("Player2 choose a deck (enter the option number)");
		choice = scan.nextInt();
		player2.setCurrentDeck( game.getDecks().get(choice).clone());
		player2.getCurrentDeck().setOwner(player2);
		
		game.CoinToss();
		player1.setHand(new Hand(game, player1));
		player2.setHand(new Hand(game, player2));
		
		player1.draw(5);
		player2.draw(5);
		
		
		game.setPlaying(true);
		
		String input = "";
		while (game.isPlaying())
		{
			//clearConsole();
			
			boolean turn =true;
			Player current = game.getCurrentTurn();
			Player off = game.getOffTurn();
			
		
			//DRAW PHASE
			current.draw(1);
			while (turn) {
			printStatus(current,off,game);
			printField(current,off,game);
			current.printHandSimple();
			System.out.println("What do you want to do?");
			input = scan.next();
		    
		    if (input.equals("end")) turn = false;
		    else cp.parse(input);
			}
			//MAIN PHASE
			//TODO: ASK THE PLAYER TO PLAY CARDS AND SUCH
			
			
			//BATTLE PHASE
			//TODO ASK THE PLAYER IF HE OR SHE WANTS TO ATTACK 
			
			//MAIN PHASE 2
			//TODO ASK THE PLAYER IF HE OR SHE WANTS TO PLAY MORE CARDS
			
			//END PHASE
			if (game.checkWin() == null) 
				game.nextTurn();
			else {
				System.out.println(game.checkWin().getName() + " wins!");
				System.out.println("Game Over!");
				game.setPlaying(false);
			}
			
		}
		
		
		
		
		
	
		
		
		
		

	}

}
